/*
战斗结果计算
*/
window._MFR = {

    quaArr:['SS','S','A','B','C'],
    stoneFtype:['金','木','水','火','土'],

    animalJob:{
        "野兽·武":1,
        "王兽·武":1,
        "灵兽·武":1,
        "圣兽·武":1,
        "野兽·御":1,
        "王兽·御":1,
        "灵兽·御":1,
        "圣兽·御":1,
        "野兽·敏":1,
        "王兽·敏":1,
        "灵兽·敏":1,
        "圣兽·敏":1, 
        "野兽·法":1,
        "王兽·法":1,
        "灵兽·法":1,
        "圣兽·法":1,
        "野兽·控":1,
        "王兽·控":1,
        "灵兽·控":1,
        "圣兽·控":1, 
        //二周目野兽
        "妖兽·武":1,
        "妖兽·御":1,
        "妖兽·敏":1, 
        "妖兽·法":1,
        "妖兽·控":1, 
        "魔兽·武":1,
        "魔兽·御":1,
        "魔兽·敏":1, 
        "魔兽·法":1,
        "魔兽·控":1, 
    },

    /*
    战斗类型
    normalFight 野外战斗
    cityFight   城战
    studyFight  学艺
    killFight   暗杀
    killBossFight 火并
    pkFight     切磋
    */

    isPeoPleFight:function(){
        for(var i=0;i<fightData.armyTeam.length;i++){
            var player = fightData.armyTeam[i];
            var job = player.job;
            if(_MFR.animalJob[job]){
                return false;
            }
        }
        return true;
    },

    getResult:function(){

        var totalExp = 0;
        var evaLevel = 0;
        var equipArr = [];
        var skillArr = [];
        var itemArr = [];
        var fightItemArr = [];
        var baseItemArr = [];
        var isSecond = false;
        var ispeopleFight = _MFR.isPeoPleFight();

        for(var i=0;i<fightData.armyTeam.length;i++){
            var player = fightData.armyTeam[i];

            if(_DROP[player.name]){
                var dropArr = _DROP[player.name];
                for(var j=0;j<dropArr.length;j++){
                    var dropname = dropArr[j];
                    if(_M[dropname]){
                        itemArr.push(dropname);
                    }else if(_EQUIP[dropname]){
                        equipArr.push(dropname);
                    }
                }
                //itemArr = itemArr.concat(dropArr);
            }else if(_SDROP[player.name]){
                isSecond = true;
                var dropArr = _SDROP[player.name];
                for(var j=0;j<dropArr.length;j++){
                    var dropname = dropArr[j];
                    if(_FIGHTITEM[dropname]){
                        fightItemArr.push(dropname);
                    }else if(_EQUIP[dropname]){
                        equipArr.push(dropname);
                    }
                }
            }

            if(!isSecond){
                for(var equipPos in _CS.equipPos){
                    if(player.equip&&player.equip[equipPos]){
                        equipArr.push(player.equip[equipPos].type);
                    }
                }
            }

            var playerLv = _EXPMANAGER.getLevelByExp(player.exp);
            if(fightData.armyTeam[i].isboss){
                totalExp+=playerLv*playerLv;
            }else{
                totalExp+=playerLv;
            }
            evaLevel+=playerLv;
        }

        if(ispeopleFight){
            totalExp=Math.floor(totalExp*2);
        }

        evaLevel=Math.floor(evaLevel/fightData.armyTeam.length);

        _MFR.setItemByLevel(evaLevel,baseItemArr,ispeopleFight);

        return {
            totalExp:totalExp,
            evaLevel:evaLevel,
            skillArr:skillArr,
            itemArr:itemArr,
            fightItemArr:fightItemArr,
            baseItemArr:baseItemArr,
            equipArr:equipArr,
            isSecond:isSecond
        };
    },


    //根据敌人平均登记设置铁掉落
    setItemByLevel:function(evaLevel,itemArr,isPeoPleFight){
        if(isPeoPleFight){
            if(evaLevel<250){
                itemArr.push('黑铁');
            }else if(evaLevel<500){
                itemArr.push('精铁');
            }else if(evaLevel<750){
                itemArr.push('百炼铁');
            }else{
                itemArr.push('天外陨铁');
            }
        }else{
            if(evaLevel<250){
                itemArr.push('甘草');
            }else if(evaLevel<500){
                itemArr.push('灵芝');
            }else if(evaLevel<750){
                itemArr.push('龙涎香');
            }else{
                itemArr.push('天山雪莲');
            }
        }

    },


    getDropEquipPer:function(){
        var baseper  = _CS.baseDropPer;
        var dropPer = _GAMEDATAHELPER.getDropPer();
        if(dropPer){
            return baseper*dropPer;
        }else{
            return baseper;
        }
    },


    fightHasCustom:function(){

        if(
            fightData
            &&fightData.playerTeam
        ){
            for(var i=0;i<fightData.playerTeam.length;i++){
                var player = fightData.playerTeam[i];
                if(player.isCustomRole&&!player.isZhaohuan){
                    return true;
                }
            }
        }
        return false;
    },


    doResult:function(){
        var resultMessage = "战斗胜利！\n";
        var result = _MFR.getResult();

        //console.log('result',result);
        var maxlv = _PLAYERSTATUS.getLevelMax(null);
        //var hasCustom = false;
        var rewardExp = result.totalExp;
        var evaLevel = result.evaLevel;
        var equipArr = result.equipArr;
        var itemArr = result.itemArr;
        var fightItemArr = result.fightItemArr;
        var baseItemArr = result.baseItemArr;
        var isSecond = result.isSecond;
        var totalgold = 0;
        var goldPer = _GAMEDATAHELPER.getGoldPer();
        var expEffectPer = _GAMEDATAHELPER.getExpPer();

        //更新任务
        for(var i=0;i<fightData.armyTeam.length;i++){
            var temparmy = fightData.armyTeam[i];
            var armyData = _ROLEDATA.getRoleDataOfPlayer(temparmy);
            _MFR.updateWork(temparmy.name,armyData.lv);
            totalgold+=armyData.lv*100;
        }

        if(goldPer){
            totalgold=Math.floor(totalgold*goldPer);
        }
        _GAMEDATAHELPER.oprMoney(totalgold);
        resultMessage+="获得钱:"+totalgold+"\n";


        if(expEffectPer){
            rewardExp=Math.floor(rewardExp*expEffectPer)
        }

        

        if(
            _HDLVHELPER.getHdLv()==0
            ||!_MFR.fightHasCustom()
        ){//非普通难度雇佣角色无法获得经验
            for(var i=0;i<fightData.playerTeam.length;i++){
                var player = fightData.playerTeam[i];
                if(player.hp>0&&!player.isCustomRole){
                    var playerLv = _EXPMANAGER.getLevelByExp(player.exp);
                    var oldexp = player.exp;
                    var expPer = evaLevel>playerLv?(1+Math.floor((evaLevel-playerLv)/5)):1;
                    var realExp = rewardExp*expPer;
                    var addPlayerExpResult = _GAMEDATAHELPER.addExp(player,realExp,maxlv);
                    var afterlv = addPlayerExpResult.afterlv;
                    if(afterlv>playerLv){
                        resultMessage+=player.name+"获得经验:"+(player.exp-oldexp)+" 等级升为:"+afterlv+"\n";
                    }else if(player.exp>oldexp){
                        resultMessage+=player.name+"获得经验:"+(player.exp-oldexp)+"\n";
                    }
                }
            }
        }

        //计算佣金
        for(var i=0;i<fightData.playerTeam.length;){
            var player = fightData.playerTeam[i];
            if(player.isCustomRole&&!player.isZhaohuan){
                var playerLv = _EXPMANAGER.getLevelByExp(player.exp);
                var paygold = playerLv*1000;
                if(paygold>window.gameData.gold){
                    window.gameData.gold=0;
                    resultMessage+="余额不足支付客将:"+player.name+"佣金"+paygold+",离开了队伍...\n";
                    var kjnpclog = _GAMEDATAHELPER.getNpcLog(player.name);
                    if(kjnpclog){
                        _GAMEDATAHELPER.addLevelData('levelDark',1);
						_GAMEDATAHELPER.addLevelData('levelBright',-100);
                        kjnpclog.f=0;
                        kjnpclog.zm=0;
                        kjnpclog.gy=0;
                        kjnpclog.diq=0;
                    }
                    fightData.playerTeam.splice(i,1);
                }else{
                    _GAMEDATAHELPER.oprMoney(-paygold);
                    resultMessage+="支付客将:"+player.name+"佣金:"+paygold+"\n";
                    i++;
                }
            }else{
                if(player.isZhaohuan){
                    fightData.playerTeam.splice(i,1);
                }else{
                    i++;
                }
            }
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.type=='normalFight'
        ){

            var tempdropPer = _MFR.getDropEquipPer();

            if(equipArr.length>0&&Math.random()<tempdropPer){

                var randomEquip = _CU.randomObj(equipArr);
                var randomEquipObj = _IOF.createObjByName(randomEquip,_MFR.randomEquipParam());
                if(isSecond){
                    _MEAA.addRandomAttrToEquip(randomEquipObj);
                }
                if(randomEquipObj){
                    resultMessage+="获得装备:<color="+_CS.qua[randomEquipObj.qua].color+">"+randomEquip+"</color>\n";
                    _GAMEDATAHELPER.pushItem(randomEquipObj);
                }
                
            }

            if(fightItemArr.length>0&&Math.random()<tempdropPer){
                var randomItem = _CU.randomObj(fightItemArr);
                var randomItemObj = _IOF.createObjByName(randomItem,null);
                var itemConfig = _FIGHTITEM[randomItem];
                resultMessage+="获得道具:<color="+_CS.qua[itemConfig.qua].color+">"+randomItem+"</color>\n";
                _GAMEDATAHELPER.pushItem(randomItemObj);
            }

            if(baseItemArr.length>0&&Math.random()<tempdropPer){
                var randomItem = _CU.randomObj(baseItemArr);
                if(_HDLVHELPER.getHdLv()==0){
                    var randomItemObj = _IOF.createObjByName(randomItem,null);
                    var itemConfig = _M[randomItem];
                    resultMessage+="获得材料:<color="+_CS.qua[itemConfig.qua].color+">"+randomItem+"</color>\n";
                    _GAMEDATAHELPER.pushItem(randomItemObj);
                }else{
                    var getItemNmb = 1+Math.floor(Math.random()*4);
                    for(var itemi=0;itemi<getItemNmb;itemi++){
                        var randomItemObj = _IOF.createObjByName(randomItem,null);
                        _GAMEDATAHELPER.pushItem(randomItemObj);
                    }
                    var itemConfig = _M[randomItem];
                    resultMessage+="获得材料:<color="+_CS.qua[itemConfig.qua].color+">"+randomItem+"</color>"+getItemNmb+"个\n";
                }
            }

            if(itemArr.length>0&&Math.random()<tempdropPer){
                var randomItem = _CU.randomObj(itemArr);
                if(_HDLVHELPER.getHdLv()==0){
                    var randomItemObj = _IOF.createObjByName(randomItem,null);
                    var itemConfig = _M[randomItem];
                    resultMessage+="获得材料:<color="+_CS.qua[itemConfig.qua].color+">"+randomItem+"</color>\n";
                    _GAMEDATAHELPER.pushItem(randomItemObj);
                }else{
                    var getItemNmb = 1+Math.floor(Math.random()*4);
                    for(var itemi=0;itemi<getItemNmb;itemi++){
                        var randomItemObj = _IOF.createObjByName(randomItem,null);
                        _GAMEDATAHELPER.pushItem(randomItemObj);
                    }
                    var itemConfig = _M[randomItem];
                    resultMessage+="获得材料:<color="+_CS.qua[itemConfig.qua].color+">"+randomItem+"</color>"+getItemNmb+"个\n";
                }
            }

            if(
                window.fightData.fightParam.bossFightMap
                &&window.fightData.fightParam.bossFightx
                &&window.fightData.fightParam.bossFighty
            ){
				_GAMEDATAHELPER.addLevelData('levelBright',50);
                _GAMEDATAHELPER.killBossSign(
                    window.fightData.fightParam.bossFightMap,
                    window.fightData.fightParam.bossFightx,
                    window.fightData.fightParam.bossFighty);
            }
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.type=='cityFight'
        ){
            var tempCityName = window.fightData.fightParam.cityName;
            window.gameData.defaultMap = tempCityName;
            window.gameData.targetPoint = window.fightData.fightParam.citytarget;
            window.gameData.cityLog[tempCityName]=window.gameData.country;
            var oldlevel = window.gameData.levelFight;
            _GAMEDATAHELPER.addLevelData('levelFight',50);
            _GAMEDATAHELPER.addLevelData('levelBright',10);
            resultMessage+='占领了:'+tempCityName;
            resultMessage+='功勋上升:'+(window.gameData.levelFight-oldlevel);
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.npc
            &&window.fightData.fightParam.type=='pkFight'
        ){  
            //console.log('执行战斗奖励');
            _GAMEDATAHELPER.addNpcF(window.fightData.fightParam.npc,20);
            resultMessage+=window.fightData.fightParam.npc+'友好度+20';
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.npc
            &&window.fightData.fightParam.type=='zhaomuFight'
        ){  
            //console.log('执行战斗奖励');
            _GAMEDATAHELPER.zhaomuNpc(window.fightData.fightParam.npc);
            resultMessage+=window.fightData.fightParam.npc+'加入我军';

        }

        
        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.npc
            &&window.fightData.fightParam.type=='studyFight'
        ){  
            var enableSkillArr = _GAMEDATAHELPER.getEnableSkillOfNpc(window.fightData.fightParam.npc);
            //_GAMEDATAHELPER.addNpcF(window.fightData.fightParam.npc,1);
            //resultMessage+=window.fightData.fightParam.npc+'友好度+1';
            if(enableSkillArr&&enableSkillArr.length>0){
                var skillname = _CU.randomObj(enableSkillArr);
                var randomItemObj = _IOF.createObjByName(skillname,null);
                window.gameData.skillLog[skillname]=1;
                _GAMEDATAHELPER.pushItem(randomItemObj);
                resultMessage+="获得技能:"+skillname;
            }
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.npc
            &&window.fightData.fightParam.type=='killFight'
        ){
            var npclog = _GAMEDATAHELPER.getNpcLog(window.fightData.fightParam.npc);
            if(npclog){
                npclog.die=1;
                //window.gameData.levelDark+=100;
                _GAMEDATAHELPER.addLevelData('levelDark',100);
                resultMessage+="击杀:"+window.fightData.fightParam.npc;
                if(_GAMEDATAHELPER.hasTeamSkill('taotie')){
                    var eatresult = _GAMEDATAHELPER.eatpeople();
                    if(eatresult){
                        resultMessage+="\n"+eatresult;
                    }
                }
            }
        }

        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.npc
            &&window.fightData.fightParam.type=='killBossFight'
        ){
            var npclog = _GAMEDATAHELPER.getNpcLog(window.fightData.fightParam.npc);
            if(npclog){
                npclog.die=1;
                _GAMEDATAHELPER.addLevelData('levelDark',100);
                resultMessage+="击杀:"+window.fightData.fightParam.npc;
                if(_GAMEDATAHELPER.hasTeamSkill('taotie')){
                    var eatresult = _GAMEDATAHELPER.eatpeople();
                    if(eatresult){
                        resultMessage+="\n"+eatresult;
                    }
                }
            }
            window.gameData.isMaster = true;
            window.gameData.master = null;
        }

        var storyResult = _MFR.getStoryFightReward();
        if(storyResult){
            resultMessage+=storyResult;
        }

        _GAMEDATAHELPER.reduceEffect('exp');
        _GAMEDATAHELPER.reduceEffect('gold');
        _GAMEDATAHELPER.reduceEffect('drop');
        return resultMessage;

    },

    getStoryFightReward:function(){

        //console.log('获取剧情战奖励：',window.fightData);



        if(
            window.fightData
            &&window.fightData.fightParam
            &&window.fightData.fightParam.type=='hideStory'
            &&_HSTORY[window.fightData.fightParam.story]
        ){
			
			//标记剧情已触发
			_GAMEDATAHELPER.logStory(window.fightData.fightParam.story);
			
            var resultMessage = "";
            var storyConf = _HSTORY[window.fightData.fightParam.story];
            var rewardConf = storyConf.reward;
            if(storyConf.isdie){
                resultMessage+=storyConf.army.join(',')+" 阵亡";
                for(var i=0;i<storyConf.army.length;i++){
                    var storynpc = storyConf.army[i];
                    var npclog = _GAMEDATAHELPER.getNpcLog(storynpc);
                    if(npclog){
                        npclog.die=1;
                    }
                }
            }
            if(rewardConf){
                for(var i=0;i<rewardConf.length;i++){
                    var tempreward = rewardConf[i];
                    if(tempreward.type=="addAttr"){
                        var addAttr = tempreward.attr;
                        var npcname = tempreward.name;
                        for(var j=0;j<window.gameData.playerTeam.length;j++){
                            if(window.gameData.playerTeam[j].name==npcname){
                                for(var key in addAttr){
                                    window.gameData.playerTeam[j][key]+=addAttr[key]/1;
                                    resultMessage+="\n"+npcname+" "+_CS.fivedataStr[key]+" 上升"+addAttr[key];
                                }
                            }
                        }
                    }else if(tempreward.type=="addPlayer"){
                        var npcname = tempreward.name;
                        var npclog = _GAMEDATAHELPER.getNpcLog(npcname);
                        npclog.zm = 1;
                        npclog.f = 100;
                        _GAMEDATAHELPER.addLevelData('levelBright',10);
                        var tmpplayer = _ROF.createRole(npcname);
                        tmpplayer.equip={};
                        window.gameData.playerTeamBack .push(tmpplayer);
                        resultMessage+="\n"+npcname+" 加入我军";
                    }else if(tempreward.type=="addSkill"){
                        var skillname = tempreward.name;
                        _GAMEDATAHELPER.pushItem(_IOF.createObjByName(skillname,null))
                        resultMessage+="\n获得技能:"+skillname;
                    }else if(tempreward.type=="beMaster"){
                        var siliname = tempreward.name;
                        if(_MASTER[siliname]){
                            window.gameData.country=siliname;
                            window.gameData.isMaster=true;
                            resultMessage+="\n成为了"+_MASTER[siliname].name+"的首领";
                        }
                    }
                }
            }
            return resultMessage;
        }
        return null;
    },

    randomEquipParam:function(){

        var randomPer = Math.random();
        var randomParam = {qua:'C',stone:[]};
        var qua = null;
        if(_HDLVHELPER.getHdLv()==0){
            for(var i=0;i<_MFR.quaArr.length;i++){
                var tmpqua = _MFR.quaArr[i];
                var quaper = _CS.qua[tmpqua].per;
                if(randomPer<=quaper){
                    qua = tmpqua;
                    break;
                }
            }
        }else{
            qua='SS';
        }
        if(qua){
            randomParam.qua = qua;
            //var stoneNmb = Math.floor(Math.random()*_CS.qua[qua].nmb);
            var stoneNmb = _CS.qua[qua].nmb;

            if(_HDLVHELPER.getHdLv()==2){//噩梦难度掉率纯色装备

                var tempstoneFtype = _CU.randomObj(_MFR.stoneFtype);

                for(var i=0;i<stoneNmb;i++){
                    randomParam.stone.push({ftype:tempstoneFtype,value:null});
                }

            }else{

                for(var i=0;i<stoneNmb;i++){
                    randomParam.stone.push({ftype:_CU.randomObj(_MFR.stoneFtype),value:null});
                }

            }
        }
        
        //console.log(randomPer,randomParam);
        return randomParam;

    },

    updateWork:function(npcname,lv){
        if(
            window.gameData
            &&window.gameData.workList
        ){
            for(var i=0;i<window.gameData.workList.length;i++){
                var tempwork = window.gameData.workList[i];
                if(tempwork.type=="fight"){
                    for(var j=0;j<tempwork.target.length;j++){
                        var targetobj = tempwork.target[j];
                        if(targetobj.lv){
                            if(targetobj.type==npcname&&targetobj.lv<=lv){
                                targetobj.current++;
                            }
                        }else{
                            if(targetobj.type==npcname){
                                targetobj.current++;
                            }
                        }
                    }
                }
            }
        }  
    },


    updateChapterWork:function(chapter,lv){
        if(
            window.gameData
            &&window.gameData.workList
        ){
            for(var i=0;i<window.gameData.workList.length;i++){
                var tempwork = window.gameData.workList[i];
                if(
                    tempwork.type=="passChapter"
                    &&tempwork.target.chapter==chapter
                    &&tempwork.target.chapterIndex==lv
                ){
                    tempwork.target.isPass = true;
                }
            }
        }  
    }

}